﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NumberParser
{
    public partial class NumberParser : Form
    {
        Dictionary<int, Digit> patterns = new Dictionary<int, Digit>();
        string[] readLines;
        IDecode decoder;
        public NumberParser()
        {
            InitializeComponent();
            decoder = new DecodeNumbers();
            InitializePatterns();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            var lines = decoder.ReadFile(txtPath.Text);
            StringBuilder sb = new StringBuilder();
            foreach (var item in lines)
            {
                sb.AppendLine(item);
            }
            MessageBox.Show(sb.ToString(), "File successfully read");

            readLines = lines;
        }

        private void InitializePatterns()
        {
            #region #5
            //#5
            Digit five = new Digit(5);
            //line 1
            var newLine = new Line();
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            five.AddLine(newLine);
            //line 2
            newLine = new Line();
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square(' ', false));
            five.AddLine(newLine);
            //line 3
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            five.AddLine(newLine);
            //line 4
            newLine = new Line();
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('|', true));
            five.AddLine(newLine);
            #endregion
            patterns.Add(5, five);
            #region #4
            //#4
            Digit four = new Digit(4);
            //line 1
            newLine = new Line();
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            four.AddLine(newLine);
            //line 2
            newLine = new Line();
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('|', true));
            four.AddLine(newLine);
            //line 3
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            four.AddLine(newLine);
            //line 4
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            four.AddLine(newLine);
            #endregion
            patterns.Add(4, four);
            #region #3
            //#4
            Digit three = new Digit(3);
            //line 1
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square(' ', false));
            three.AddLine(newLine);
            //line 2
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('/', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            three.AddLine(newLine);
            //line 3
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('\\', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            three.AddLine(newLine);
            //line 4
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square(' ', false));
            three.AddLine(newLine);
            #endregion
            patterns.Add(3, three);
            #region #2
            //#2
            Digit two = new Digit(2);
            //line 1
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square(' ', false));
            two.AddLine(newLine);
            //line 2
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('_', true));
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            two.AddLine(newLine);
            //line 3
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            two.AddLine(newLine);
            //line 4
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square('-', true));
            newLine.AddSquare(new Square(' ', false));
            two.AddLine(newLine);
            #endregion
            patterns.Add(2, two);
            #region #1
            //#1
            Digit one = new Digit(1);
            //line 1
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            one.AddLine(newLine);
            //line 2
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            one.AddLine(newLine);
            //line 3
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            one.AddLine(newLine);
            //line 4
            newLine = new Line();
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square('|', true));
            newLine.AddSquare(new Square(' ', false));
            newLine.AddSquare(new Square(' ', false));
            one.AddLine(newLine);
            #endregion
            patterns.Add(1, one);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (var item in patterns)
            {
                textBox1.Clear();
                textBox1.Text = item.Value.ToString();
                MessageBox.Show(item.Key.ToString());
                textBox1.Clear();
            }
        }

        List<Digit> decodeResult;
        private void btnPatternMatch_Click(object sender, EventArgs e)
        {
            decodeResult = decoder.Decode(readLines);

            List<int> finalResult = new List<int>();
            if (decodeResult != null && decodeResult.Count > 0)
            {
                foreach (var item in decodeResult)
                {
                    var found = false;
                    foreach (var pattern in patterns)
                    {
                        if (pattern.Value.Matches(item))
                        {
                            found = true;
                            finalResult.Add(pattern.Key);
                            break;
                        }
                    }
                    if (!found)
                        finalResult.Add(-1);
                }
            }
            string rslt = string.Empty;
            foreach (var item in finalResult)
            {
                rslt += item == -1 ? "NaN" : item.ToString() + "   ";
            }
            MessageBox.Show(rslt);
        }

        private void btnSaveToFile_Click(object sender, EventArgs e)
        {
            var toWrite = new Dictionary<int, List<Digit>>();
            //parse the textbox and search for digits
            for (int i = 0; i < txtFilePattern.Lines.Length; i++)
            {
                foreach (var character in txtFilePattern.Lines[i])
                {
                    if (char.IsDigit(character))
                    {
                        var digit = int.Parse(character.ToString());
                        if (patterns.ContainsKey(digit))
                        {
                            if (toWrite.ContainsKey(i))
                                toWrite[i].Add(patterns[digit]);
                            else
                            {
                                var lst = new List<Digit>();
                                lst.Add(patterns[digit]);
                                toWrite.Add(i, lst);
                            }
                        }
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            //go th´rough each "digit line"
            foreach (var digitLine in toWrite)
            {
                //go through each line
                for (int i = 0; i < 4; i++)
                {
                    //build a file line
                    string l = string.Empty;
                    //go through each digit
                    foreach (var digit in digitLine.Value)
                    {
                        l += digit.Lines[i].ToString() + "   ";
                    }
                    sb.AppendLine(l.Substring(0, l.Length - 3));
                }
            }
            try
            {
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(txtPathToSave.Text))
                {
                    file.WriteLine(sb.ToString());
                }
            }
            catch (Exception ex)
            {
                // Let the user know what went wrong.
                System.Windows.Forms.MessageBox.Show("The file could not be read: " + ex.Message);
            }
        }

        private void txtFilePattern_KeyPress(object sender, KeyPressEventArgs e)
        {
            //allow only digits from 1 to 5 and 'return' key
            byte digit = 0;
            e.Handled = !((char.IsDigit(e.KeyChar) && byte.TryParse(e.KeyChar.ToString(), out digit) && digit > 0 && digit < 6)
                || char.IsControl(e.KeyChar));
        }

    }
}
